package com.sunlg.smarthome.ui.light;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.LightContract;
import com.sunlg.smarthome.contract.presenter.LightPresenter;
import com.sunlg.smarthome.data.entity.Light;

import butterknife.BindView;

public class LightActivity extends BaseActivity implements LightContract.View{

    @BindView(R.id.rl_light_bg)
    RelativeLayout rlLightBg;
    @BindView(R.id.sb_light)
    SeekBar sbLight;

    private Light mLight;
    private int mPosition;
    private int status;
    private LightContract.Presenter presenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_light;
    }

    @Override
    protected void init() {
        Bundle bundle = getIntent().getExtras();
        mLight = (Light) bundle.getSerializable("light");
        mPosition = bundle.getInt("pos");
        presenter = new LightPresenter();
        presenter.attachView(this);
    }

    @Override
    protected void initView() {
        status = mLight.getStatus();
        System.out.println(status);
        sbLight.setProgress(status);

        if(status == 0){
            rlLightBg.setBackgroundResource(R.color.colorPrimaryDark);
        }else if(status > 0){
            rlLightBg.setBackgroundResource(R.color.colorPrimary);
            rlLightBg.setAlpha(Float.intBitsToFloat(status));
        }


        rlLightBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(status == 0){   //为关状态，则打开
                    status = mLight.getStatus();
                    presenter.takeOn(mLight.getId(), status);
                    takeOnSuccess();
                }else if(status > 0){  //为开状态，则关闭
                    status = 0;
                    presenter.takeOff(mLight.getId());
                    takeOffSuccess();
                }
            }
        });

        sbLight.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                status = progress;

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if(status > 0){
                    presenter.takeOn(mLight.getId(), status);
                    takeOnSuccess();
                }else {
                    presenter.takeOff(mLight.getId());
                    takeOffSuccess();
                }
            }
        });
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(mLight.getDescription());
        actionBar.setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    public void takeOnSuccess() {
        sbLight.setProgress(status);
        mLight.setStatus(status); //保存照明设备状态


        float lum = 255 - (255/100)*status;

        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setScale(lum, lum, lum, 1);

        Drawable drawable = getResources().getDrawable(R.color.colorPrimary);
        drawable.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        rlLightBg.setBackground(drawable);
    }

    @Override
    public void takeOffSuccess() {
        sbLight.setProgress(0);
        rlLightBg.setBackgroundResource(R.color.colorPrimaryDark);
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //发送照明设备状态变化广播
        Intent intent = new Intent(C.Action.LIGHT_STATUS_CHANGED);
        Bundle bundle = new Bundle();
        bundle.putInt("status", status);
        bundle.putInt("pos", mPosition);
        intent.putExtras(bundle);
        sendBroadcast(intent);

        presenter.dettachView();
    }
}
